this.open = (function () {

  var open = {};

  var logUtils$2 = {
      showing: false,
      show () {
        console.show ();
        this.showing = true;
      },
      hide () {
        console.hide ();
        this.showing = false;
      },
      log () {
        console.log.apply (console, arguments);
      },
    };

  var gUtils$3 = {
      random (min, max) {
        return random (min, max);
      },
      sleep (start, end) {
        var duration = this.random (start, end) * 1000;
        sleep (duration);
      },
      sleepB (duration) {
        sleep (duration);
      },
      possible (start, end, scale) {
        var scale = scale || 100;
        var v = this.random (1, scale);
        return v >= start && v <= end;
      },
      hasExec (startTime) {
        var now = Date.now ();
        return parseInt ((now - startTime) / 1000);
      },
    };

  var deviceUtils$2 = {
    lockScreen() {
      var cmd = "input keyevent 26";
      $shizuku(cmd);
    },
    back() {
      var cmd = "input keyevent 4";
      $shizuku(cmd);
    },
    home() {
      var cmd = "input keyevent 3";
      $shizuku(cmd);
    },
    recents() {
      var cmd = "input keyevent 187";
      $shizuku(cmd);
    },
  };

  var clickUtils$2 = {
    getRateX(x, w) {
      return parseInt(x * (device.width / (w || device.width)));
    },
    getRateY(y, h) {
      return parseInt(y * (device.height / (h || device.height)));
    },
    clickXY(x, y) {
      var cmd = "input tab " + x + " " + y;
      $shizuku(cmd);
    },
    clickXYZW(x, y, z, w, deltaX, deltaY) {
      var maxDeltaX = Math.abs(z - x) / 2;
      var maxDeltaY = Math.abs(w - y) / 2;
      deltaX = deltaX || 1;
      deltaY = deltaY || 1;
      deltaX = deltaX > maxDeltaX ? maxDeltaX - 2 : deltaX;
      deltaY = deltaY > maxDeltaY ? maxDeltaY - 2 : deltaY;
      this.clickXY(
        random(x + deltaX, z - deltaX),
        random(y + deltaY, w - deltaY)
      );
    },
    clickBounds(bounds, deltaX, deltaY) {
      var left = bounds.left;
      var right = bounds.right;
      var top = bounds.top;
      var bottom = bounds.bottom;
      this.clickXYZW(left, top, right, bottom, deltaX, deltaY);
    },
    clickPointMap(pointMap, deltaX, deltaY) {
      var point = pointMap[0];
      var width = pointMap[1];
      var height = pointMap[2];
      var x = this.getRateX(point[0], width);
      var y = this.getRateY(point[1], height);
      var z = this.getRateX(point[2], width);
      var w = this.getRateY(point[3], height);
      this.clickXYZW(x, y, z, w, deltaX, deltaY);
    },
    clickTextUI(TextUI, deltaX, deltaY) {
      this.clickBounds(TextUI.bounds(), deltaX, deltaY);
    },
    clickTextOCR(TextOCR, deltaX, deltaY) {
      this.clickBounds(TextOCR.bounds, deltaX, deltaY);
    },
  };

  var swipeUtils$2 = {
    swipe(a, b, c, d, duration) {
      var cmd = "input swipe " + a + " " + b + " " + c + " " + d + " " + duration;
      $shizuku(cmd);
    },
  };

  var logUtils$1 = logUtils$2;
  var gUtils$2 = gUtils$3;
  var deviceUtils$1 = deviceUtils$2;
  var clickUtils$1 = clickUtils$2;
  var swipeUtils$1 = swipeUtils$2;
  var pointMap = {
    关闭所有应用: [[420, 2006, 660, 2246], 1080, 2340],
  };
  var appUtils$2 = {
    appName: null,
    openApp(appName) {
      this.appName = appName;
      var result = app.launchApp(appName);
      logUtils$1.log(appName.length);
      logUtils$1.log("打开：", appName, result ? "成功" : "失败");
      sleep(2 * 1000);
      return result;
    },
    closeRecent() {
      deviceUtils$1.home();
      sleep(2 * 1000);
      deviceUtils$1.recents();
      sleep(2 * 1000);
      if (["cannon", "evergo"].includes(device.product)) {
        swipeUtils$1.swipe(
          device.width / 4 + device.width / 8 + device.width / 16,
          device.height / 2,
          device.width / 20,
          device.height / 2,
          200
        );
      } else {
        swipeUtils$1.swipe(
          device.width / 2,
          device.height / 2,
          device.width / 2,
          device.height / 16,
          gUtils$2.random(300, 500)
        );
      }
      sleep(2 * 1000);
      deviceUtils$1.home();
      sleep(2 * 1000);
    },
    closeAll() {
      deviceUtils$1.home();
      sleep(2 * 1000);
      deviceUtils$1.recents();
      sleep(2 * 1000);
      clickUtils$1.clickPointMap(pointMap.关闭所有应用);
      sleep(2 * 1000);
    },
    checkInstall(appName) {
      return !!app.getPackageName(appName);
    },
    home(appName) {
      logUtils$1.log("将回到桌面");
      home();
      gUtils$2.sleep(10, 15);
      this.openApp(appName);
    },
  };

  var logUtils = logUtils$2;
  var gUtils$1 = gUtils$3;
  var deviceUtils = deviceUtils$2;
  var appUtils$1 = appUtils$2;
  var clickUtils = clickUtils$2;
  var swipeUtils = swipeUtils$2;
  // var textUtils = require("./text/textUtils");
  // var ocrUtils = require("./ocr/ocrUtils");

  var shizukuUtils = {
    logUtils: logUtils,
    gUtils: gUtils$1,
    deviceUtils: deviceUtils,
    appUtils: appUtils$1,
    clickUtils: clickUtils,
    swipeUtils: swipeUtils,
    // textUtils: textUtils,
    // ocrUtils: ocrUtils,
  };

  var { appUtils, gUtils } = shizukuUtils;

  function main(appName, type) {
    if ($shizuku.isRunning()) {
      var closeRecent = true;
      var closeAll = false;
      if (type == 1) {
        closeRecent = false;
        closeAll = false;
      }
      if (type == 2) {
        closeRecent = false;
        closeAll = true;
      }
      if (closeRecent) {
        appUtils.closeRecent();
      }
      if (closeAll) {
        appUtils.closeAll();
      }
      if (appName) {
        gUtils.sleep(2, 3);
        appUtils.openApp(appName);
      }
    } else {
      console.show();
      console.log("shizuku 未运行，请检查");
    }
  }
  Wang.main = main;

  return open;

})();
